package com.yan.deezer.data.manager;

import java.util.HashMap;
import java.util.List;

import android.graphics.Bitmap;
import android.widget.ImageView;

import com.yan.deezer.data.object.Album;
import com.yan.deezer.data.object.Artist;
import com.yan.deezer.data.object.Track;
import com.yan.deezer.network.HttpService;
import com.yan.deezer.utils.Debug;
import com.yan.deezer.utils.AsyncImageSetter;

public class DataManager {

	private static final String TAG = "YAN_DEEZER_DATA_MANAGER";

	private static DataManager sInstance;

	public static DataManager getInstance() {
		if (sInstance == null) {
			sInstance = new DataManager();
		}
		return sInstance;
	}

	private static HashMap<Integer, Bitmap> imageCacheForAlbums;
	private static HashMap<Integer, Bitmap> imageCacheForArtists;

	private DataManager() {
		imageCacheForAlbums = new HashMap<Integer, Bitmap>();
		imageCacheForArtists = new HashMap<Integer, Bitmap>();
	}

	public List<Artist> getArtists(String userInput) {
		List<Artist> artists = null;
		try {
			artists = HttpService.getArtistList(userInput);
		} catch (Exception e) {
			Debug.debugPermanent(TAG,
					"Exception : getArtists -> " + e.getMessage());
			e.printStackTrace();
			return null;
		}
		return artists;
	}

	public List<Album> getArtistAlbums(String artistID) {
		List<Album> albums = null;
		try {
			albums = HttpService.getArtistAlbumList(artistID);
		} catch (Exception e) {
			Debug.debugPermanent(TAG,
					"Exception : getArtistAlbums -> " + e.getMessage());
			e.printStackTrace();
			return null;
		}
		return albums;
	}

	public List<Track> getAlbumTracks(String albumID) {
		List<Track> tracks = null;
		try {
			tracks = HttpService.getAlbumTrackList(albumID);
		} catch (Exception e) {
			Debug.debugPermanent(TAG,
					"Exception : getAlbumTracks -> " + e.getMessage());
			e.printStackTrace();
			return null;
		}
		return tracks;
	}

	/*************************
	 * 
	 * YAN DEEZER CACHE UTILS
	 * 
	 ************************/

	public void setBitmapToImageView(String url, ImageView imageView, int id,
			HashMap<Integer, Bitmap> imageCache) {

		if (imageCache.containsKey(id)) {
			Bitmap bmp = imageCache.get(id);
			if (bmp != null)
				imageView.setImageBitmap(bmp);
		} else {
			new AsyncImageSetter(imageView, id, imageCache).execute(url);
		}
	}

	public HashMap<Integer, Bitmap> getImageCacheForArtists() {

		return imageCacheForArtists;
	}

	public HashMap<Integer, Bitmap> getImageCacheForAlbums() {

		return imageCacheForAlbums;
	}

	public void addBitmap(int imgID, Bitmap b,
			HashMap<Integer, Bitmap> imageCache) {

		imageCache.put(imgID, b);

	}

}
